package com.twetec.cms.adapter.pc.backend.tag;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.twetec.cms.adapter.pc.backend.tag.annotation.TplTag;
import com.twetec.cms.adapter.pc.backend.tag.doc.TagDocObject;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.model.po.website.ColumnPO;
import com.twetec.cms.model.po.website.SitePO;
import com.twetec.cms.service.website.ColumnService;
import com.twetec.cms.service.website.SiteColumnService;
import com.twetec.cms.service.website.SiteService;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.tag.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service()
@Scope("prototype")
@TplTag("columnList")
public class ColumnListTag extends GeneralVarTagBinding {

    @Autowired
    private SiteService siteService;

    @Autowired
    private ColumnService columnService;

    @Autowired
    private SiteColumnService siteColumnService;

    @Override
    public void render() {
        System.out.println("ColumnListTag");

        // 接收标签参数
        Map result = Maps.newHashMap();
        // 站点id
        Long siteId = (this.getAttributeValue("siteId") instanceof String) ?
                Long.parseLong((String) this.getAttributeValue("siteId")) : (Long) this.getAttributeValue("siteId");

        // 栏目类型
        String type = (String) this.getAttributeValue("type");

        // 父栏目编码id
        Long parentColumnId = (this.getAttributeValue("parentColumnId") instanceof String) ?
                Long.parseLong((String) this.getAttributeValue("parentColumnId")) : (Long) this.getAttributeValue("parentColumnId");

		// 标记是预览checkPage还是静态化staticPage/null
//		String pageType = (String) this.getAttributeValue("pageType");

        // 资讯发布系统的域名
//        String serverUrl = (String) this.getAttributeValue("serverUrl");

        // 获取站点信息
        SitePO sitePO = siteService.getById(siteId);

        if (!ValidateUtil.isNullOrEmpty(sitePO)) {

            List<ColumnPO> columnPOList = new ArrayList<>();

            // 如果传入的columnId不是0，就获取该栏目下的子栏目信息
            QueryWrapper<ColumnPO> childColumnQueryWrapper = new QueryWrapper<>();
            childColumnQueryWrapper.eq("PARENT_ID", parentColumnId);
            if (StringUtils.isNotEmpty(type)) {
                childColumnQueryWrapper.eq("TYPE", type);
            }
            childColumnQueryWrapper.eq("IS_RECOVERY", 0);
            childColumnQueryWrapper.orderByDesc("SORT");
            childColumnQueryWrapper.orderByAsc("ID");
            columnPOList = columnService.list(childColumnQueryWrapper);

            // 如果查询到的栏目集合不为空
            if (!ValidateUtil.isNullOrEmpty(columnPOList)) {

                // 遍历展示的栏目集合
                for (int i = 0; i < columnPOList.size(); i++) {

//                    String jumpUrl = sitePO.getDomainUrl();
                    String jumpUrl = "";

                    jumpUrl = jumpUrl + columnPOList.get(i).getEncodedXpath() + File.separator + "index.html";


                    String name = columnPOList.get(i).getName();
                    String columnCode = columnPOList.get(i).getEncoded();
                    String columnType = columnPOList.get(i).getType();
                    String logo = columnPOList.get(i).getLogo();
                    // 返回标签参数
                    result.put("name", name);
                    result.put("encoded", columnCode);
                    result.put("logo", logo);
                    result.put("type", columnType);
//                    if (null != pageType && pageType.equals("checkPage")) {
//                        result.put("url", serverUrl + "website/column/check-column-page?columnId=" + parentColumnId
//                                + "&siteId=" + siteId + "&childColumnId=" + columnPOList.get(i).getId() +"&page=1");
//                    } else {
						result.put("url", jumpUrl);
//					}
                    result.put("columnId", columnPOList.get(i).getId());

                    this.binds(result);
                    this.doBodyRender();
                }
            }
        }
    }


    /**
     * 标签文档
     *
     * @return
     */
    public static TagDocObject doc() {
        TagDocObject tagDocObject = new TagDocObject();
        tagDocObject.setTagName("columnList");
        tagDocObject.setDesc("栏目列表标签");
        tagDocObject.setExample("<#columnList serverUrl=\"${serverUrl}\" siteId = \"${site.siteId!}\" parentColumnId=\"${parentColumnId!}\" pageType=\"${pageType!}\" type=\"\" var=\"data\">" +
                "        <li>${data.++!}</li>\n" +
                "      </#navList>");
        tagDocObject.setParamDesc("siteId：所属站点id\n" +
                "serverUrl：资讯发布系统的域名或IP和Port\n" +
                "dm：站点域名\n" +
                "pageType：用于标识是生成静态页面还是预览（预览：checkPage，生成：null或staticPage）\n" +
                "columnId：所属栏目id\n" +
                "--------- var: 标签的数据结果集 ---------\n" +
                "name：栏目名称\n" +
                "encoded：栏目编码\n" +
                "type：栏目类型\n" +
                "jumpUrl：跳转地址");

        tagDocObject.setOtherDesc("无");

        return tagDocObject;
    }

}
